﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;

namespace StudentManage.Web.Admin.Management.SysManage.DataSynList
{
    public partial class XQDM_Syn : System.Web.UI.Page
    {
        private static DataTable dt_Add = new DataTable();
        private static DataTable dt_Up = new DataTable();
        private static List<string> Idlist = new List<string>();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;
        }

        //绑定要插入的数据
        private void RepInsertBind()
        {
            if (dt_Add == null || dt_Add.Rows.Count <= 0)
            {
                this.Rep_Insert.DataSource = null;
                this.Rep_Insert.DataBind();
            }
            else
            {
                int RowCount = dt_Add.Rows.Count;
                this.AspNetPager_Insert.RecordCount = RowCount;
                int PageSize = this.AspNetPager_Insert.PageSize;
                int SkipCount = PageSize * (this.AspNetPager_Insert.CurrentPageIndex - 1);

                List<DataRow> list = dt_Add.Rows.Cast<DataRow>().Skip(SkipCount).Take(PageSize).ToList();
                this.Rep_Insert.DataSource = list.CopyToDataTable();
                this.Rep_Insert.DataBind();
            }
        }

        //绑定要修改的数据
        private void RepUpdateBind()
        {
            if (dt_Up == null || dt_Up.Rows.Count <= 0)
            {
                this.Rep_Update.DataSource = null;
                this.Rep_Update.DataBind();
            }
            else
            {
                int RowCount = dt_Up.Rows.Count;
                this.AspNetPager_Update.RecordCount = RowCount;
                int PageSize = this.AspNetPager_Update.PageSize;
                int SkipCount = PageSize * (this.AspNetPager_Update.CurrentPageIndex - 1);

                List<DataRow> list = dt_Up.Rows.Cast<DataRow>().Skip(SkipCount).Take(PageSize).ToList();
                this.Rep_Update.DataSource = list.CopyToDataTable();
                this.Rep_Update.DataBind();
            }
        }

        //检测数据
        protected void Btn_UpLoad_Click(object sender, EventArgs e)
        {
            new DataSyn().GetData("xqdm", "XQDM",ref dt_Add, ref dt_Up,ref Idlist);
            this.lab_InCount.Text = (dt_Add != null ? dt_Add.Rows.Count.ToString() : "0");
            this.lab_UpCount.Text = (dt_Up != null ? dt_Up.Rows.Count.ToString() : "0");
            this.Btn_Exec.Enabled = true;
            this.lab_status.Text = "检测完毕";
            this.RepInsertBind();
            this.RepUpdateBind();
        }

        //同步数据
        protected void Btn_Exec_Click(object sender, EventArgs e)
        {
            this.Btn_Exec.Enabled = false;
            DataTable dt = new DataTable();
            dt.Columns.Add("XQDM", typeof(string));
            dt.Columns.Add("XQMC", typeof(string));
            for (int i = 0; i < dt_Add.Rows.Count; i++)
            {
                DataRow row = dt.NewRow();
                row["XQDM"] = dt_Add.Rows[i]["XQDM"].ToString();
                row["XQMC"] = dt_Add.Rows[i]["XQMC"].ToString();
                dt.Rows.Add(row);
            }
            new DataSyn().SqlBulkCopyInsert(dt, "xqdm");
            new DataSyn().UpdateData("xqdm", "XQDM", dt_Up, Idlist);

            this.lab_status.Text = "执行完毕!";
            this.lab_InCount.Text = "";
            this.lab_UpCount.Text = "";
            this.Rep_Insert.DataSource = null;
            this.Rep_Insert.DataBind();
            this.Rep_Update.DataSource = null;
            this.Rep_Update.DataBind();
            this.AspNetPager_Insert.RecordCount = 0;
            this.AspNetPager_Update.RecordCount = 0;
        }

        //绑定事件
        protected void Rep_Insert_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Del")
            { 
                string key=e.CommandArgument.ToString();
                dt_Add.Rows.Remove(dt_Add.Select("XQDM='" + key + "'").First());
                this.RepInsertBind();
            }
        }

        //绑定事件
        protected void Rep_Update_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Del")
            {
                string key = e.CommandArgument.ToString();
                dt_Up.Rows.Remove(dt_Up.Select("XQDM='" + key + "'").First());
                this.RepUpdateBind();
            }
        }

        protected void AspNetPager_Insert_PageChanged(object sender, EventArgs e)
        {
            this.RepInsertBind();
        }

        protected void AspNetPager_Update_PageChanged(object sender, EventArgs e)
        {
            this.RepUpdateBind();
        }
    }
}